package dst1.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import dst1.listener.DefaultEntityListener;


@Entity
@Table(name="CLUSTER",
	uniqueConstraints = @UniqueConstraint(columnNames={"NAME"}))
public class Cluster implements Serializable{
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="ID") 
	private Long id;
	
	@Column(name="NAME", unique=true) 
	private String name;
	
	@Column(name="LASTSERVICE")
	//@Temporal(TemporalType.TIMESTAMP)
	private Date lastService;
	
	@Column(name="NEXTSERVICE") 
	//@Temporal(TemporalType.TIMESTAMP)
	private Date nextService;
	
	@OneToMany(mappedBy="cluster", fetch=FetchType.LAZY)
	private List<Computer> computers;
	
	@ManyToMany
	@JoinTable(
        name="CLUSTERS",
        joinColumns={@JoinColumn(name="CHILDCLUSTERS_ID", referencedColumnName="ID", nullable=false)},
        inverseJoinColumns={@JoinColumn(name="PARENTCLUSTERS_ID", referencedColumnName="ID", nullable=false)}
    )
	private List<Cluster> childClusters;
	
	@ManyToMany (mappedBy="childClusters")
	private List<Cluster> parentClusters;
	
	@ManyToOne(cascade=CascadeType.ALL)
	private Grid grid;
	
	@ManyToOne(cascade=CascadeType.ALL)
	private Admin admin;
	
	public Cluster(){
		
	}

	public Cluster(String name, Date lastService, Date nextService,
			List<Computer> computers, List<Cluster> childClusters,
			List<Cluster> parentClusters, Grid grid, Admin admin) {
		super();
		this.name = name;
		this.lastService = lastService;
		this.nextService = nextService;
		this.computers = computers;
		this.childClusters = childClusters;
		this.parentClusters = parentClusters;
		this.grid = grid;
		this.admin = admin;
	}

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getLastService() {
		return lastService;
	}

	public void setLastService(Date lastService) {
		this.lastService = lastService;
	}

	public Date getNextService() {
		return nextService;
	}

	public void setNextService(Date nextService) {
		this.nextService = nextService;
	}

	public List<Computer> getComputers() {
		return computers;
	}

	public void setComputers(List<Computer> computers) {
		this.computers = computers;
	}

	public List<Cluster> getChildClusters() {
		return childClusters;
	}

	public void setChildClusters(List<Cluster> childClusters) {
		this.childClusters = childClusters;
	}

	public List<Cluster> getParentClusters() {
		return parentClusters;
	}

	public void setParentClusters(List<Cluster> parentClusters) {
		this.parentClusters = parentClusters;
	}

	public Grid getGrid() {
		return grid;
	}

	public void setGrid(Grid grid) {
		this.grid = grid;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	
	
}
